import scrapy
# 导入TestprojectItem
from testproject.items import TestprojectItem
 
class PriceSpider(scrapy.Spider):
    # 爬虫的名字
    name = "price"
    # 允许的域名
    allowed_domains = ["techlabplt.com"]
    # 起始的URL地址
    start_urls = ["http://www.techlabplt.com:8080/BD-PC/priceList?pageId=1"]
 
    # 在parse方法内实现爬取数据的逻辑
    def parse(self, response):
        # 获取当前页内的所有农副产品的相关信息
        price_list = response.xpath("//*[@id='tableBody']/tr")
        # 遍历price_list 获取每一项内的数据值
        for price in price_list:
            #tmp = {}
            tmp = TestprojectItem()
            # 判断选择器对象的值是不是存在，如果存在就提取出来，不存在赋空置
            if price.xpath("./td[1]/text()"):
                tmp['top_class'] = price.xpath("./td[1]/text()")[0].extract()
            else:
                tmp['top_class'] = ''
            if price.xpath("./td[2]/text()"):
                tmp['sec_class'] = price.xpath("./td[2]/text()")[0].extract()
            else:
                tmp['sec_class'] = ''
            if price.xpath("./td[3]/text()"):
                tmp['product_name'] = price.xpath("./td[3]/text()")[0].extract()
            else:
                tmp['product_name'] = ''
            if price.xpath("./td[4]/text()"):
                tmp['low_price'] = price.xpath("./td[4]/text()")[0].extract()
            else:
                tmp['low_price'] = ''
            if price.xpath("./td[5]/text()"):
                tmp['avg_price'] = price.xpath("./td[5]/text()")[0].extract()
            else:
                tmp['avg_price'] = ''
            if price.xpath("./td[6]/text()"):
                tmp['high_price'] = price.xpath("./td[6]/text()")[0].extract()
            else:
                tmp['high_price'] = ''
            if price.xpath("./td[7]/text()"):
                tmp['specs'] = price.xpath("./td[7]/text()")[0].extract()
            else:
                tmp['specs'] = ''
            if price.xpath("./td[8]/text()"):
                tmp['origin'] = price.xpath("./td[8]/text()")[0].extract()
            else:
                tmp['origin'] = ''
            if price.xpath("./td[9]/text()"):
                tmp['unit'] = price.xpath("./td[9]/text()")[0].extract()
            else:
                tmp['unit'] = ''
            if price.xpath("./td[10]/text()"):
                tmp['update_date'] = price.xpath("./td[10]/text()")[0].extract()
            else:
                tmp['update_date'] = ''
            # 返回数据
            yield tmp
